//
//  TypeViewController.m
//  体育商城
//
//  Created by  on 2022/9/23.
//

#import "TypeViewController.h"
#import "GoodsCell.h"
#import "BuyViewController.h"
#import "SearchViewController.h"
@interface TypeViewController ()
@property (strong, nonatomic) IBOutlet BaseTextField *input;
@property (strong, nonatomic) IBOutlet SegView *segView;
@property (strong, nonatomic) IBOutlet BaseTextField *search;
@property (nonatomic, strong) NSArray <Goods*>*array;
@end

@implementation TypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.search endEditing:YES];
}
- (void)initUI{
    self.title = LS(@"分类");
    self.backBtn.hidden = YES;
    self.array = [GoodsTool find:@"全部" count:0];
    WS(self)
    self.segView.block = ^(NSString * _Nonnull text) {
        if ([text isEqualToString:LS(@"全部")]) {
            weakself.array = [GoodsTool find:@"全部" count:0];
        }else if ([text isEqualToString:LS(@"球鞋")]){
            weakself.array = [GoodsTool find:@"球鞋" count:0];
        }else if ([text isEqualToString:LS(@"篮球")]){
            weakself.array = [GoodsTool find:@"篮球" count:0];
        }else if ([text isEqualToString:LS(@"足球")]){
            weakself.array = [GoodsTool find:@"足球" count:0];
        }else if ([text isEqualToString:LS(@"足球衫")]){
            weakself.array = [GoodsTool find:@"足球衫" count:0];
        }
    };
    
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"搜索"]];
    image.frame = CGRectMake(16, 10, 24, 24);
    self.input.leftView = image;
    self.input.leftViewMode = UITextFieldViewModeAlways;
    [self.collectionView registerNib:[GoodsCell nib] forCellWithReuseIdentifier:[GoodsCell ID]];
    NSLayoutConstraint *top = [self.collectionView a1:NSLayoutAttributeTop e:NSLayoutRelationEqual t:self.segView a2:NSLayoutAttributeBottom c:0];
    NSLayoutConstraint *left = [self.collectionView a1:NSLayoutAttributeLeft e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeLeft c:0];
    NSLayoutConstraint *right = [self.collectionView a1:NSLayoutAttributeRight e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeRight c:0];
    NSLayoutConstraint *bottom = [self.collectionView a1:NSLayoutAttributeBottom e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeBottom c:0];
    [self.view addConstraints:@[top,left,right,bottom]];
}

- (void)setArray:(NSArray<Goods *> *)array{
    _array = array;
    [self.collectionView reloadData];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    SearchViewController *vc = [SearchViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodsCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[GoodsCell ID] forIndexPath:indexPath];
    cell.model = self.array[indexPath.row];
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    BuyViewController *vc = [BuyViewController new];
    vc.model = self.array[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat w = (SCREEN_WIDTH - 20 * 3) / 2;
    CGFloat h = 240.0 / 168 * w;
    return  CGSizeMake(w,h);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(20, 20, 20, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

@end
